<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
    <script src="./libs/http.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章类别管理
        </div>
        <div class="container-fluid common_con">
            <table class="table table-striped table-bordered table-hover mp20 category_table">
                <thead>
                    <tr>
                        <th>名称</th>
                        <th>Slug</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>奇趣事</td>
                        <td>funny</td>
                        <td class="text-center">
                            <a href="javascript:void(0)" data-toggle="modal" class=" btn btn-info btn-xs">编辑</a>
                            <a href="javascript:void(0)" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" class="text-center">
                            <a href="javascript:void(0)" class="btn btn-success" id="xinzengfenlei" data-toggle="modal"
                                data-target="#myModal">新增分类</a>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    <!-- 模态框 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">新增分类</h4>
                </div>
                <div class="modal-body">
                    <form id="form">
                        <input type="hidden" name="id" id='cate_id'>
                        <div class="form-group">
                            <label for="recipient-name" class="control-label">分类名称:</label>
                            <input type="text" class="form-control" id="recipient-name" name="name">
                        </div>
                        <div class="form-group">
                            <label for="message-text" class="control-label">分类别名:</label>
                            <input class="form-control" id="message-text" name="slug">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default cancel">取消</button>
                    <button type="button" class="btn btn-primary confirm">确认</button>
                </div>
            </div>
        </div>
    </div>
    <script id="art_cat" type="text/html">
        {{ each data v }}
        <tr>
            <td>{{ v.name }}</td>
            <td>{{ v.slug }}</td>
            <td class="text-center">
                <a href="javascript:;" data-toggle="modal" data-target="#myModal" class=" btn btn-info btn-xs" data-name="{{v.name}}" data-slug="{{v.slug}}" data-id="{{ v.id }}">编辑</a>
                <a href="javascript:;" class="btn btn-danger btn-xs delete" data-id="{{ v.id }}">删除</a>
            </td>
        </tr>
        {{ /each }}
    </script>
    <script>
        //1.页面一加载发送ajax请求数据
        $.ajax({
            url: BigNew.category_list,
            type: 'get',
            dataType: 'json',
            success: function (backData) {
                console.log(backData);
                //模板引擎渲染页面 
                $('.category_table>tbody').html(template('art_cat', backData));
            }
        });
        // 当模态框出现之前触发
        $('#myModal').on('show.bs.modal', function (e) {
            console.log(e.relatedTarget)
            var target = e.relatedTarget;
            console.log(e.relatedTarget);
            if (target == $('#xinzengfenlei')[0]) {
                //执行新增分类逻辑
                // alert('你点击了新增按钮');
                //(1)设置按钮文本为新增
                $('#myModal .confirm').text('新增');
            } else {
                //执行编辑分类逻辑
                // alert('你点击了编辑按钮');
                //(1)设置按钮文本为编辑
                $('#myModal .confirm').text('编辑');
                // 填充默认数据
                let data = target.dataset
                console.log(data)
                $('#recipient-name').val(data.name)
                $('#message-text').val(data.slug)
                $('#cate_id').val(data.id)
            }
        })
        //2.取消按钮点击事件
        $('#myModal .cancel').on('click', function (e) {
            $('#form')[0].reset();
            //隐藏模态框
            $('#myModal').modal('hide')
        });
        //3.确认按钮点击事件
        $('#myModal .confirm').on('click', function (e, a) {
            // do something...
            if ($(this).text() == '新增') {
                $.ajax({
                    url: BigNew.category_add,
                    type: 'post',
                    dataType: 'json',
                    data: {
                        name: $('#recipient-name').val(),
                        slug: $('#message-text').val(),
                    },
                    success: function (backData) {
                        if (backData.code == 201) {
                            alert('新增成功');
                            window.location.reload();
                        } else {
                            alert(backData.msg);
                        };
                    }
                });
            } else {//编辑
                $.ajax({
                    url: BigNew.category_edit,
                    type: 'post',
                    dataType: 'json',
                    data: $('#form').serialize(),
                    success: function (backData) {
                        if (backData.code == 200) {
                            alert('编辑成功');
                            window.location.reload();
                        } else {
                            alert(backData.msg);
                        };
                    }
                });
            }
        });
        $('.category_table>tbody').on('click', '.delete', function () {
            $.ajax({
                url: BigNew.category_delete,
                type: 'post',
                dataType: 'json',
                data: {
                    id: $(this).attr('data-id')
                },
                success: function (backData) {
                    if (backData.code == 204) {
                        alert('删除成功');
                        window.location.reload();
                    } else {
                        alert(backData.msg);
                    };
                }
            });
        });
    </script>
</body>

</html>